﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Facade;
using IronOne.SalesOne.BusinessObjects;


namespace IronOne.SalesOne.SalesOneWeb.SalesActivity
{
    public partial class Search : SOSecurityPage
    {
        StructureFacade facade;
        int userType;

        protected void Page_Load(object sender, EventArgs e)
        {
            facade = new StructureFacade();            

            if (!Page.IsPostBack)
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                User user = facade.GetUser(User.Identity.Name);
                //User user = facade.GetUser("ss");
                UserInformation info = facade.GetUserInformation(User.Identity.Name);
                userType = info.UserType;

                bool flag = false;  

                if (userType == 4)
                {
                    /*if (user.SoCode != null)
                    {
                        ddlSO.Items.Add(user.SoCode);
                        ddlSO.SelectedIndex = ddlSO.Items.Count - 1;

                        ddlSO.Enabled = false;
                    }
                    else*/
                        populateSOCodes(facade.GetSOsInUnit(user.NodeId));                    
                    flag = true;
                }

                if (flag == true)
                {
                    ddlUnit.Items.Add(info.UnitName);
                    ddlUnit.SelectedIndex = ddlUnit.Items.Count -1;
                    
                    ddlUnit.Enabled = false;
                }
                else if (userType == 3)
                {
                    populateUnits(facade.GetUnitsForBranch(user.NodeId));
                    flag = true;
                }

                if (flag == true)
                {
                    ddlBranch.Items.Add(info.BranchName);
                    ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;

                    ddlBranch.Enabled = false;
                }
                else if (userType == 2)
                {
                    populateBranches(facade.GetBranchesForGroup(user.NodeId));
                    flag = true;
                }

                if (userType == 1)
                    populateBranches(facade.GetBranches("BranchName ASC"));

                //populateBranches(facade.GetBranches("BranchName ASC"));

                populateMonths();
                ddlMonth.SelectedValue = DateTime.Now.Month.ToString();
                
                populateYears();
                ddlYear.SelectedValue = DateTime.Now.Year.ToString();
                
                setControls();
            }
        }


        private void setControls()
        {
            /*if (ddlBranch.SelectedIndex == 0)
            {
                ddlUnit.Enabled = false;
                ddlUnit.SelectedIndex = 0;
            }
            else
            {
                ddlUnit.Enabled = true;
            }

            if (ddlUnit.SelectedIndex <= 0)
            {
                ddlSO.Enabled = false;
                ddlSO.SelectedIndex = 0;
            }
            else
            {
                ddlSO.Enabled = true;
            }

            if (ddlSO.SelectedIndex <= 0)
            {
                btnSearch.Enabled = false;
            }
            else
            {
                btnSearch.Enabled = true;
            }*/


           
            /*if ((userType == 2 || userType == 1) && ddlBranch.SelectedIndex == 0)
            {
                ddlUnit.Enabled = false;
                ddlUnit.SelectedIndex = 0;
            }
            else
            {
                ddlUnit.Enabled = true;
            }

            if (userType == 3 && ddlUnit.SelectedIndex <= 0)
            {
                ddlSO.Enabled = false;
                ddlSO.SelectedIndex = 0;
            }
            else
            {
                ddlSO.Enabled = true;
            }

            if (userType == 4 && ddlSO.SelectedIndex <= 0)
            {
                btnSearch.Enabled = false;
            }
            else
            {
                btnSearch.Enabled = true;
            }*/
            if(ddlSO.SelectedIndex <= 0)
                btnSearch.Enabled = false;
        }

        private void populateBranches(List<Branch> branches)
        {            
            ddlBranch.DataSource = branches;           
            ddlBranch.DataTextField = "BranchName";
            ddlBranch.DataValueField = "BranchID";
            ddlBranch.DataBind();

            addDefaultItem(ddlBranch);
        }

        private void populateMonths()
        {
            DateTimeFormatInfo d = new DateTimeFormatInfo();
            
            ddlMonth.DataSource = d.MonthNames;
            for (int i = 1; i <= 12; i++)
            {
                ListItem item = new ListItem(d.MonthNames[i - 1], i.ToString());
                ddlMonth.Items.Add(item);
            }
            //addDefaultItem(ddlMonth);
        }

        private void populateYears()
        {
            DateTime dt = DateTime.Now.AddYears(1);
            for (int i = 0; i < 10; i++)
            {
                ddlYear.Items.Add((dt.Year - i).ToString());
            }
            ddlYear.SelectedValue = DateTime.Now.Year.ToString();
            //addDefaultItem(ddlYear);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (ddlSO.SelectedIndex > 0)
            {
                DateTime firstDay = new DateTime(int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue), 1);
                DateTime lastDay = GetLastDayOfMonth(int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue));

                Response.Redirect("~/SalesActivity/ActivityPlan.aspx?from=" + firstDay.ToShortDateString() + "&to=" + lastDay.ToShortDateString() + "&SOCode=" + ddlSO.SelectedValue);
                //Response.Redirect("~/SalesActivity/ActivityPlan.aspx?from=" + firstDay.ToShortDateString() + "&to=" + lastDay.ToShortDateString() + "&SOCode=1212");
            }
            else
            {
                Response.Write("Plese select a Sales Consultance first.");
            }

        }


        private DateTime GetLastDayOfMonth(int year, int iMonth)
        {
            DateTime dtTo = new DateTime(year, iMonth, 1);
            return dtTo.AddDays(DateTime.DaysInMonth(year, iMonth) - 1);
        }

        protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlBranch.SelectedIndex > 0)
            {
                populateUnits(facade.GetUnitsInBranch(int.Parse(ddlBranch.SelectedValue)));
                ddlUnit.SelectedIndex = 0;
                ddlSO.SelectedIndex = 0;   
            }
            setControls();
        }

        private void populateUnits(List<SalesOneUnit> units)
        {
            ddlUnit.DataSource = units;
            ddlUnit.DataTextField = "Name";
            ddlUnit.DataValueField = "UnitID";
            ddlUnit.DataBind();

            addDefaultItem(ddlUnit);            
        }

        private void addDefaultItem(DropDownList ddl)
        {
            ListItem newItem = new ListItem("Select...");            
            ddl.Items.Insert(0,newItem);        
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUnit.SelectedIndex > 0)
            {
                populateSOCodes(facade.GetSOsInUnit(int.Parse(ddlUnit.SelectedValue)));                
            }
            setControls();
        }

        private void populateSOCodes(List<string> users)
        {
            ddlSO.Items.Clear();
            foreach (string str in users)
            {
                ddlSO.Items.Add(str);                
            }
            addDefaultItem(ddlSO);
        }

        protected void ddlSO_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSO.SelectedIndex > 0)
            {
                btnSearch.Enabled = true;                
            }
            setControls();
        }

    }
}